/* 
 * File:   Button.cpp
 * Author: agu
 * 
 * Created on 26 de mayo de 2014, 02:15
 */

#include "client_Button.h"
#include <iostream>
#include "client_Directories.h"
using std::cout;
using std::endl;

Button::Button(SDL_Renderer* renderer, const string path, const string effect,
        int x, int y):texture(renderer, path, x, y), effect(effect) {
    currentSprite = BUTTON_SPRITE_MOUSE_DOWN;

    spriteClips[ BUTTON_SPRITE_MOUSE_DOWN ].x = 0;
    spriteClips[ BUTTON_SPRITE_MOUSE_DOWN ].y = 0;
    spriteClips[ BUTTON_SPRITE_MOUSE_DOWN ].w = BUTTON_WIDTH;
    spriteClips[ BUTTON_SPRITE_MOUSE_DOWN ].h = BUTTON_HEIGHT;

    spriteClips[ BUTTON_SPRITE_MOUSE_UP ].x = 0;
    spriteClips[ BUTTON_SPRITE_MOUSE_UP ].y = BUTTON_HEIGHT;
    spriteClips[ BUTTON_SPRITE_MOUSE_UP ].w = BUTTON_WIDTH;
    spriteClips[ BUTTON_SPRITE_MOUSE_UP ].h = BUTTON_HEIGHT;
    //Set sprites
    for (int i = 0; i < BUTTON_SPRITE_TOTAL; ++i) {
        spriteClips[ i ].x = 0;
        spriteClips[ i ].y = i * BUTTON_HEIGHT;
        spriteClips[ i ].w = BUTTON_WIDTH;
        spriteClips[ i ].h = BUTTON_HEIGHT;
    }
    this->runner=NULL;
    
}

Button::~Button() {
    if(this->runner != NULL)
        delete this->runner;
}

void Button::update(SDL_Event* event) {
    //If mouse event happened
    if (event->type == SDL_MOUSEMOTION || event->type == SDL_MOUSEBUTTONDOWN || event->type == SDL_MOUSEBUTTONUP) {
        //Get mouse position
        int x, y;
        SDL_GetMouseState(&x, &y);
        //Check if mouse is in button
        bool inside = true;
        //Mouse is left of the button
        if (x < this->texture.getX()) {
            inside = false;
        }//Mouse is right of the button
        else if (x > this->texture.getX() + this->texture.getWidth()) {
            inside = false;
        }//Mouse above the button
        else if (y < this->texture.getY()) {
            inside = false;
        }//Mouse below the button
        else if (y > this->texture.getY() + this->texture.getHeight()) {
            inside = false;
        }
        //Mouse is outside button
        if (!inside) {
            currentSprite = BUTTON_SPRITE_MOUSE_DOWN;
        }//Mouse is inside button
        else {
            //Set mouse over sprite
            switch (event->type) {
                case SDL_MOUSEBUTTONDOWN:
                    this->keyNames->push("00011|BUTTONDOWN");        
                    this->currentSprite = BUTTON_SPRITE_MOUSE_DOWN;
                    this->effect.play();
                    if(this->runner != NULL){
                    }                        
                    break;
                case SDL_MOUSEBUTTONUP:
                    this->keyNames->push("00009|BUTTONUP");        
                    this->currentSprite = BUTTON_SPRITE_MOUSE_UP;
                    break;
            }
        }
    }
}

void Button::render() {
    //Show current button sprite
    texture.render(&spriteClips[currentSprite]);
}

void Button::setPosition(int x, int y) {
    this->texture.setPosition(x, y);
}

void Button::setRunner(Runner* runner){
    this->runner=runner;
}

void Button::setQueue(queue<string> *instrucciones){
    this->keyNames = instrucciones;
}
